// pages/roomCreat/roomCreat.js
const app = getApp()
var that;
var util = require('../../utils/util.js');
Page({

  data: {
    roomObj: {
      "companyId": "",
      "computer": true,
      "createTime": "",
      "description": "",
      "id": "",
      "name": "",
      "person":"",
      "pic": "",
      "projector": true,
      "status": 0,
      "updateTime": ""
    },
    loadingType: false, //报名防止连续点击
  },

  onLoad: function (options) {
    that = this;
    
  },

  //名称
  inpNameFun: function (e) {
    that.data.roomObj.name = e.detail.value;
    that.setData({
      roomObj: that.data.roomObj
    })
  },

  // 点击上传会议室照片
  upLogoFun: function () {
    wx.chooseImage({
      count: 1,
      sizeType: ['original', 'compressed'],
      sourceType: ['album', 'camera'],
      success(res) {
        console.log("chooseImage", res)

        util.upFileFun(res.tempFilePaths[0]).then(
          function (res) {
            console.log('upLoadFun suc', res)
            if (res.statusCode == 200) {
              var datas = JSON.parse(res.data);
              that.data.roomObj.pic = datas.data;
              that.setData({
                roomObj: that.data.roomObj
              })
            }
          }
        ).catch(
          function (err) {
            console.log('upLoadFun fail', err)
          }
        );
      }
    })
  },

  //会议室描述
  inpDescriptionFun: function (e) {
    that.data.roomObj.description = e.detail.value;
    that.setData({
      roomObj: that.data.roomObj
    })
  },

  //可容纳人数
  inpPersonFun: function (e) {
    that.data.roomObj.person = e.detail.value;
    that.setData({
      roomObj: that.data.roomObj
    })
  },

  // 是否有电脑
  radioChange:function(e){
    console.log("e",e)
    if (e.detail.value == "true") {
      that.data.roomObj.computer = true;
    } else {
      that.data.roomObj.computer = false;
    }
    that.setData({
      roomObj: that.data.roomObj
    })
  },

  // 是否有投影
  radioChange2: function (e) {
    console.log("e", e)
    if (e.detail.value == "true"){
      that.data.roomObj.projector = true;
    }else{
      that.data.roomObj.projector = false;
    }
    
    that.setData({
      roomObj: that.data.roomObj
    })
  },

  //创建按钮点击 先判断信息填写是否全面
  creatRoomFun:function(){
    if (that.data.roomObj.name == "") {
      wx.showToast({
        title: '请填写名称',
        icon: 'none'
      })
    } else if (that.data.roomObj.pic == "") {
      wx.showToast({
        title: '请上传照片',
        icon: 'none'
      })
    } else if (that.data.roomObj.description == "") {
      wx.showToast({
        title: '请填写描述',
        icon: 'none'
      })
    } else if (that.data.roomObj.person == "") {
      wx.showToast({
        title: '请填写可容纳人数',
        icon: 'none'
      })
    }
    else {
      if (that.data.loadingType == false) {
        that.setData({
          loadingType: true
        })
        that.saveBtnRegFun();
      }
    }
  },

  // 创建接口/v0.1/api/hysMeetingRoom/addMeetingRoom
  saveBtnRegFun: function () {

    wx.request({
      url: app.http + 'hysMeetingRoom/addMeetingRoom?companyId=' + app.globalData.loginUserObj.ydCompany.id,
      data: that.data.roomObj,
      method: 'POST',
      header: {
        'content-type': 'application/json', // 默认值
        'APP-User-Token': app.globalData.tokenMap.access_token
      },
      success: function (res) {
        console.log("底部确认suc返回", res)
        if (res.data.code == 200) {
          var pages = getCurrentPages();

          var prevPage = pages[pages.length - 2]; //上一个页面
          if (prevPage.route == "pages/roomSet/roomSet") {
            prevPage.getRoomListFun();
          }
          wx.showToast({
            title: '创建成功',
            success: function () {
              setTimeout(function () {
                wx.navigateBack({
                  delta:1
                })
              }, 1000)
            }
          })
        }
        else if (res.data.code == 100101) {
          // token无效，获取新的token
          var newToken = util.refreshTokenFun(app.globalData.tokenMap.refresh_token).then(
            function (res) {
              if (res.data.code == 200) {
                that.saveBtnRegFun();
              }
              else if (res.data.code == 400) {
                var wxLoginFun = util.wxLoginFun().then(
                  function (res2) {
                    if (res2.data.code == 200) {
                      that.saveBtnRegFun();
                    }
                  }
                )
              }
            }
          ).catch(
            function (err) {
              console.log("获取新的token失败返回", err)
            }
          )
        }
      },
      fail: function (err) {
        console.log("底部确认fail返回", err)
      },
      complete: function () {
        that.setData({
          loadingType: false
        })
      }
    })
  }


})